<!--
 * @Author: wuyue
 * @Date: 2023-02-23 16:49:17
 * @LastEditTime: 2023-03-23 17:46:19
 * @LastEditors: wuyue
 * @Descripttion: 
 * @version: 
-->
<template>
  <div>
    <a-card :bordered="false">
      <title-name v-if="isToDo" title="业务详情"></title-name>
      <a-card :bordered="false">
        <title-name title="基础信息"></title-name>
        <custom-form :is-emit="false" :formList="formList" :form="form" editModel="" :formComponents="$refs"> </custom-form>
      </a-card>
      <a-card :bordered="false">
        <title-name title="计量设备调拨申请单"></title-name>
        <a-button v-if="editModel !== 0" style="margin-bottom: 10px;" type="primary" @click="addEquipmentAllocateItem"> 增行 </a-button>
        <a-table :columns="columns" bordered :data-source="form.inventoryList" :pagination="false">
          <template slot="mdId" slot-scope="text, record">
              <a-select v-if="!isToDo" v-model="record.mdId" :disabled="editModel === 0" @change="(val) => getSpecificationModel(val, record)">
                  <a-select-option v-for="i in equipmentArr" :key="i.id">
                  {{ i.mdName }}
                  </a-select-option>
              </a-select>
              <span v-else><a-input disabled v-model="record.mdName"></a-input></span>
          </template>
          <template slot="mdModelId" slot-scope="text, record">
              <a-select v-if="!isToDo" v-model="record.mdModelId" :disabled="editModel === 0">
                  <a-select-option v-for="i in record.specificationModelArr" :key="i.id">
                  {{ i.specificationModel }}
                  </a-select-option>
              </a-select>
              <span v-else><a-input disabled v-model="record.specificationModel"></a-input></span>
          </template>
          <template slot="applyNum" slot-scope="text, record">
              <BaseInputNumber :isInt="true" :disabled="editModel === 0" v-model="record.applyNum" placeholder="请输入申请数量"  />
          </template>
          <template slot="remarks" slot-scope="text, record">
              <BaseTextarea placeholder="请输入备注" :disabled="editModel === 0" v-model="record.remarks" />
          </template>
          <template slot="action" slot-scope="text, record, index">
            <a-button type="link" @click="deleteItem(record, index)"> 删除 </a-button>
          </template>
        </a-table>
      </a-card>
    </a-card>
  </div>
</template>

<script>

export default {
  props: {
    form: Object,
    editModel: [Number, String],
    isToDo: Boolean,
    equipmentArr: Array
  },
  data() {
    const columns = [
        {
            title: '序号',
            width: '65px',
            customRender: (text, row , index) => index+1
        },
        {
            title: '设备名称',
            dataIndex: 'mdId',
            width: '250px',
            scopedSlots: { customRender: 'mdId' }
        },
        {
            title: '规格型号',
            dataIndex: 'mdModelId',
            width: '250px',
            scopedSlots: { customRender: 'mdModelId' }
        },
        {
            title: '申请数量（个）',
            dataIndex: 'applyNum',
            width: '250px',
            scopedSlots: { customRender: 'applyNum' }
        },
        {
            title: '调拨数量（个）',
            dataIndex: 'transferNum',
            width: '250px',
        },
        {
            title: '落实数量（个）',
            dataIndex: 'executeNum',
            width: '250px',
        },
        {
            title: '备注',
            dataIndex: 'remarks',
            scopedSlots: { customRender: 'remarks' }
        },
        {
            title: '操作',
            scopedSlots: { customRender: 'action' }
        }
    ]
    let columnsData = this.$clone(columns)
    if(this.editModel === 0) {
      columnsData.splice(columnsData.length-1, 1)
    }
    if(this.editModel !== 0 || this.isToDo) {
      columnsData.splice(4, 2)
    }
    return {
      formList: [
        {
          keyName: this.isToDo ? 'transferInUnitName' : 'transferInUnit',
          type: this.isToDo ? 'a-input' :'orgTree',
          label: '调入单位',
          span: 16,
          isAutoSelectFirst:true,
          isNeedAuth:true,
          checkStrictly: true,
          disabled: this.editModel === 0,
        },
        {
            type: 'a-date-picker',
            keyName: 'transferCompleteTime',
            label: '调拨完成时间',
            disabled: this.editModel === 0,
            attrs: {
                valueFormat: 'YYYY-MM-DD 00:00:00',
                format: 'YYYY-MM-DD',
            }
        },
        {
            type: 'a-input',
            keyName: 'fillInUserName',
            label: '填报人',
            disabled: true
        },
        {
            type: 'a-date-picker',
            keyName: 'fillInTime',
            label: '填报时间',
            disabled: true,
            attrs: {
                valueFormat: 'YYYY-MM-DD 00:00:00',
                format: 'YYYY-MM-DD',
            }
        },
      ],
      columns: columnsData,
    }
  },
  methods: {
    // 获取对应设备的规格型号
    getSpecificationModel(val, record) {
      let row = this.equipmentArr.find(item => item.id === val)
      // 设置规格型号的值
      record.specificationModelArr = row ? row.list : []
      record.mdModelId = null
    },
    // 增加计量设备调拨
    addEquipmentAllocateItem() {
        this.form.inventoryList.push({
            mdId: null,
            mdModelId: null,
            applyNum: null,
            remarks: null,
            specificationModelArr: []
        })
    },
    // 删除
    deleteItem(row, index) {
       this.form.inventoryList.splice(index, 1)
    },
  }
}
</script>

<style>
</style>